﻿$.extend({
    popupIframe: function (title, url, callback, size) {
        if (size === undefined || size == null)
            size = ["60%", "80%"];
        parent.layer.closeAll();
        parent.layer.open({
            //btn: ['取消'],
            //btnAlign: 'r',
            type: 2,
            title: title,
            shadeClose: false,
            shade: 0.8,
            area: size,
            maxmin: true,
            content: url, //iframe的url
            end: function () {
                if (callback)
                    callback();
                //else
                //    window.location = window.location;
                location = location;
            },
            //btn1:function (index) {
            //    layer.close(index);
            //}
        });
    },
    loading: function () {
        layer.load(2);
    },
    alert: function (msg, callback) {
        layer.closeAll();

        layer.alert(msg, {
            title: jsHelper.getLanguageItemText("lblalertTitle"),
            btn: [jsHelper.getLanguageItemText("btnQueDing")],
            icon: 7,
            end: function () {
                if (callback) {
                    callback();
                    window.location = window.location;
                }
                // else 
                //window.location = window.location;
            }
        });
    },
    msg: function (msg) {
        layer.msg(msg, { icon: 1, shade: 0.01 });
    },
    open: function (obj) {
        parent.layer.open({
            type: 1,
            shade: false,
            maxmin: true,
            title: false, //不显示标题
            content: obj, //捕获的元素
            cancel: function () {

            }
        });
    },
    popup: function (obj, callback, title) {
        parent.layer.closeAll();
        parent.layer.open({
            type: 1,
            title: title ? title : false,
            closeBtn: 0,
            shadeClose: false,
            skin: 'yourclass',
            maxmin: true,
            content: (obj instanceof jQuery ? obj.html() : obj),
            end: function () {
                if (callback)
                    callback();
            },
            success: function () {

            }
        });

    },
    popupFull: function (obj) {
        parent.layer.closeAll();
        parent.layer.open({
            type: 1,
            title: false,
            closeBtn: 1,
            shadeClose: false,
            maxmin: true,
            skin: 'text-center',
            area: ["90%", "90%"],
            content: (obj instanceof jQuery ? obj.html() : obj)

        });
    },
    //confirm: function (msg, caller) {
    //    layer.msg(msg, {
    //        time: 0,
    //        btn: [jsHelper.getLanguageItemText("btnQueDing"), jsHelper.getLanguageItemText("btnQuXiao")],
    //        yes: function (index) {
    //            layer.close(index);
    //            if (caller) {
    //                caller();
    //            }
    //        }
    //    });
    //},


    //公共提示消息样式统一
    confirm: function (msg, caller) {
        if (msg.indexOf("m") == 0 || msg.indexOf("M") == 0)
            msg = msg.substr(1);
            layer.alert(msg, {
                title: jsHelper.getLanguageItemText("lblalertTitle"),//"系统提示",
            btn: [jsHelper.getLanguageItemText("btnQueDing"), jsHelper.getLanguageItemText("btnQuXiao")],
            icon: 7,
            yes: function (index) {
                layer.close(index);
                if (caller) {
                    caller();
                }
            }
        });
    },


    setQueryString: function (uri, key, value) {
        var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
        var separator = uri.indexOf('?') !== -1 ? "&" : "?";
        if (uri.match(re)) {
            return uri.replace(re, '$1' + key + "=" + value + '$2');
        }
        else {
            return uri + separator + key + "=" + value;
        }
        return uri;
    }
});
$(function () {
    $(document).on("click", ".btn-post", function () {
        var control = $(this), confirm = true;
        if (control.attr("confirm") == undefined) {
            confirm = false;
        }
        function action() {
            $.loading();

            $.post(control.data("url"), eval("(" + control.data("params") + ")"), function (response) {
                if (response.success) {
                    $.alert(response.success, function () {
                        var returnUrl = control.data("return");
                        if (returnUrl)
                            window.location = returnUrl;
                        else
                            window.location = window.location;
                    });
                } else {
                    $.alert(response.msg);
                }
            });
        }
        if (confirm) {           
            $.confirm("mC001:" + jsHelper.getLanguageItemText("mC001"), function () {
                action();
            });
        } else {
            action();
        }
    });
    $(".btn-post-check").unbind();
    $(document).on("click", ".btn-post-check", function () {
        var control = $(this), confirm = true, isRefreshFather = true;
        if (control.attr("confirm") == undefined) {
            confirm = false;
        }
        if (control.attr("isRefreshFather") == undefined) {
            isRefreshFather = false;
        }    
            var values = getCheckBoxVal(getElementsByName("input", "check-item"));
            if (values.length == 0) {
                $.alert("mC004:" + jsHelper.getLanguageItemText("mC004"));
                return;
            }
            if (confirm) {
                $.confirm("mC001:" + jsHelper.getLanguageItemText("mC001"), function () {
                    action();
                });
            } else {
                action();
            }

            function action() {
            $.loading();  
            var params = merge_options({ ids: values }, eval("(" + control.data("params") + ")"));
            $.post(control.data("url"), params, function (response) {
                if (response.success) {
                    var msg = response.msg;
                    if (msg == "") {
                        msg = "mC002:" + jsHelper.getLanguageItemText("mC002");
                    }
                    $.alert(msg, function () {
                        if (isRefreshFather) {
                            //alert("isRefreshFather")
                            window.location = window.location;
                        }
                        else {
                            //alert("111")
                            window.location = window.location;
                        }
                    });
                } else {
                    $.alert(response.msg);
                }
            });
        }

    });
   
   
   
    //$(".main-right").css("width", $(window).width() - 260);
    $(".checkAll").click(function () {
        var checked = $(this).prop("checked");
        $(this).parents("table").find("input[type=checkbox][name='check-item']").each(function (n, m)
        {
            if ($(this).prop("checked") != checked) {
                $(this).prop("checked", checked);
                $(this).change();
            }
        });
    });
    //$(".checkAll2").click(function () {
    //    var checked = $(this).prop("checked");
    //    $("input[name='check-item2']").each(function (n, m) {
    //        if ($(this).prop("checked") != checked) {
    //            $(this).prop("checked", checked);
    //            $(this).change();
    //        }
    //    });
    //});

    //$("table tbody tr input[type=checkbox]").click(function (event) {
       
    //});
    $(document).on("change", "table tbody tr input[type=checkbox]", function () {
        var tr = $(this).parent().parent();
        if ($(this).prop("checked")) {
            tr.data("bgcolor", tr.css("background-color"));
            tr.css("background-color", "rgb(250, 255, 189)");
            //单选 table可配置
            var table = tr.parent().parent();
            if (!table.attr("multi-select") || table.attr("multi-select") != "true") {
                var checkedRow = $("table tbody tr input[type=checkbox]:checkbox:checked");
                if (checkedRow.length > 1) {
                    for (var i = 0; i < checkedRow.length; i++) {
                        if (checkedRow[i].value != $(this).prop("value")) {
                            $(checkedRow[i]).prop("checked", false);
                            $(checkedRow[i]).parent().parent().css("background-color", "");
                            break;
                        }
                    }
                }
            }
        }
        else {
            if (tr.data("bgcolor") !== undefined) {
                //tr.css("background-color", tr.data("bgcolor"));
                tr.css("background-color", "");
            }
        }
    })
    $(".btn-frame").click(function () {
        var obj = $(this);
        $.popupIframe(obj.attr("title"), obj.attr("href"), null, eval("(" + obj.attr("size") + ")"));
        return false;
    });
    $(".btn-framecheckbox").unbind();
    $(".btn-framecheckbox").click(function () {
        var obj = $(this);
        var data = $(this).data();
        if ($("input[name='check-item']:checkbox:checked").length > 0) {
            if ($("input[name='check-item']:checkbox:checked").length > 1) {
                $.alert("mC003:" + jsHelper.getLanguageItemText("mC003"));
                return false;
            }
            var _href = obj.attr("href");
            //物料列表专用字段
            //alert($("input[name='check-item']:checkbox:checked").data("status"));
            //var status = $("input[name='check-item']:checkbox:checked").data("status");
            ////发料单编辑
            //if ($("input[name='check-item']:checkbox:checked").data("column") != undefined && $("input[name='check-item']:checkbox:checked").data("column") == "InvoiceEdit")
            //{
            //    if (obj.data("trxnumtype") == $("input[name='check-item']:checkbox:checked").data("trxnumtype")) {
            //        if (status != "Created" && status != "Rejected") {
            //            $.alert("只有已创建或已退回的工单允许编辑操作！");
            //            return false;
            //        }
            //    }
            //    else {
            //        $.alert("您所要编辑的信息与您所点击的按钮不符！");
            //        return false;
            //    }
            //}
            //工单作业操作 gaoqh begin
            //if (data.woOpe != undefined) {
            //    switch (data.woOpe) {
            //        case ("Modify"):
            //            if (status != "Create") {
            //                $.alert("只有Create状态的工单允许编辑操作！");
            //                return false;
            //            }
            //            break; 
            //        case ("WoQtyAdd"):
            //            if (status != "Confirm" && status != "Release") {
            //                $.alert("只有Confirm或Release状态的工单允许加量操作！");
            //                return false;
            //            }
            //            break;
            //    }
            //}
            //工单作业操作 end
            //return false;
            if (_href.indexOf("?") != -1) {
                _href = _href + "&seq=" + $("input[name='check-item']:checkbox:checked").val();
            }
            else {
                _href = _href + "?seq=" + $("input[name='check-item']:checkbox:checked").val();
            }
            $.popupIframe(obj.attr("title"), _href, null, eval("(" + obj.attr("size") + ")"));
            return false;
        }
        else {
            $.alert("mC004:" + jsHelper.getLanguageItemText("mC004"));
            return false;
        }
        //$.popupIframe(obj.attr("title"), obj.attr("href"), null, eval("(" + obj.attr("size") + ")"));
        //return false;
    });

    //区域树状图选中新增
    //$(".btn-treeaddcheckbox").click(function () {
    //    var _class = $("#fac_tree .active").attr("class");
    //    var stype = "";
    //    if (_class == "gccss active") {
    //        stype = "gc";
    //        $.alert("工厂节点下不可新增！");
    //        return false;
    //    }
    //    else if (_class == "sbcss active" || _class == "zcdsbcss active") {
    //        stype = "sb";
    //        $.alert("设备节点下不可新增！");
    //        return false;
    //    }
    //    else if (_class == "zqycss active") {
    //        stype = "zqy";
    //        $.alert("子区域节点下不可新增！");
    //        return false;
    //    }
    //    else if (_class == "zcdcss active") {
    //        stype = "zcd";
    //    }
    //    else {
    //        stype = "qy";
    //    }

    //    var obj = $(this);
    //    if ($("#fac_tree .active").length > 0) {
    //        var _href = obj.attr("href");
    //        if (_href.indexOf("?") == -1) {
    //            if (stype == "zcd") {
    //                _href = _href + "?class=" + stype + "&code=" + $("#fac_tree .active").data("id") + "&zcdid=" + $("#fac_tree .active").data("zcdid");
    //            }
    //            else if (stype == "qy") {
    //                _href = _href + "?fatherSeq=" + $("#fac_tree .active").data("id") + "&class=" + stype + "&zcdid=" + $("#fac_tree .active").data("zcdid");
    //            }
    //        }
    //        $.popupIframe(obj.attr("title"), _href, null, eval("(" + obj.attr("size") + ")"));
    //        return false;
    //    }
    //    else {
    //        $.alert("请选择要新增的父节点！");
    //        return false;
    //    }
    //});

    //区域树状图选中编辑
    //$(".btn-treecheckbox").click(function () {
    //    var _class = $("#fac_tree .active").attr("class");
    //    var stype = "";
    //    if (_class == "gccss active") {
    //        stype = "gc";
    //        $.alert("工厂节点不可编辑！");
    //        return false;
    //    }
    //    else if (_class == "sbcss active" || _class == "zcdsbcss active") {
    //        stype = "sb";
    //        $.alert("设备节点不可编辑！");
    //        return false;
    //    }
    //    else if (_class == "zcdcss active") {
    //        stype = "zcd";
    //        if ($("#fac_tree .zcdsbcss").length <= 0) {
    //            $.alert("制程段下无设备不可编辑！");
    //            return false;
    //        }
    //    }
    //    else {
    //        stype = "qy";
    //    }

    //    var obj = $(this);
    //    if ($("#fac_tree .active").length > 0) {
    //        var _href = obj.attr("href");
    //        if (stype == "zcd") {
    //            _href = _href + "?seq=&class=" + stype + "&code=" + $("#fac_tree .active").data("id") + "&type=1";
    //        }
    //        else {
    //            _href = _href + "?seq=" + $("#fac_tree .active").data("id") + "&class=" + stype + "&code=" + stype + "&type=1";
    //        }

    //        $.popupIframe(obj.attr("title"), _href, null, eval("(" + obj.attr("size") + ")"));
    //        return false;
    //    }
    //    else {
    //        $.alert("请选择要编辑的节点！");
    //        return false;
    //    }
    //});

    //$(document).on("click", ".btn-treepost", function () {
    //    var control = $(this), confirm = true;
    //    if (control.attr("confirm") == undefined) {
    //        confirm = false;
    //    }

    //    function action() {
    //        $.loading();
    //        var stype = "";
    //        var _calss = $("#fac_tree .active").attr("class");
    //        if (_calss == "gccss active") {
    //            $.alert("工厂节点不可删除！");
    //            return false;
    //        }
    //        else if (_calss == "zcdcss active") {
    //            $.alert("制程段不可删除！");
    //            return false;
    //        }
    //        else if (_calss == "zcdsbcss active") {
    //            stype = "zcdsb";
    //        }
    //        else if (_calss == "sbcss active") {
    //            $.alert("设备节点不可删除！");
    //            return false;
    //        }

    //        var values = $("#fac_tree .active").data("id");
    //        var params = merge_options({ stype: stype, seq: values }, eval("(" + control.data("params") + ")"));
    //        $.post(control.data("url"), params, function (response) {
    //            if (response.success) {
    //                $.alert(response.msg, function () {
    //                    window.location = window.location;
    //                });
    //            } else {
    //                $.alert(response.msg);
    //            }
    //        });
    //    }
    //    if (confirm) {
    //        $.confirm("确定进行此操作吗？", function () {
    //            action();
    //        });
    //    } else {
    //        action();
    //    }
    //});

    //条件检索列表导出功能
    $('.export_btn,.export').unbind();
    $('.export_btn,.export').click(function () {
        var control = $(this);
        //console.log(control.data("url"));
        layer.msg("mC005:" + jsHelper.getLanguageItemText("mC005"), {
            icon: 16
            , time: '1000'
            , offset: 't'
            , end: function () {
                var url = control.data("url");
                if (url.indexOf('?') < 0) {
                    location = url + "?" + $('#formSearch').serialize();
                }
                else {
                    location = url;
                }

                layer.msg("mC006:" + jsHelper.getLanguageItemText("mC006"), {
                    icon: 1, time: '1000', offset: 't'
                });
            }
        });

    });

    //复选列表导出功能
    $('.checkexport_btn').unbind();
    $('.checkexport_btn').click(function () {
        var control = $(this);
        var values = getCheckBoxVal(getElementsByName("input", "check-item"));        
        layer.msg("mC005:" + jsHelper.getLanguageItemText("mC005"), {
            icon: 16
            , time: '1000'
            , offset: 't'
            , end: function () {
                var url = control.data("url");
                location = url + "?ids=" + values;
                layer.msg("mC006:" + jsHelper.getLanguageItemText("mC006"), {
                    icon: 1, time: '1000', offset: 't'
                });
            }
        });

    });

    $(".btn-return").click(function () {
        //if (inIframe()) {
        //    window.parent.location = window.parent.location;
        //} else {
        //    window.location = window.location;
        //}
        parent.layer.closeAll();
    });
    $("table td>img").click(function (event) {
        event.stopPropagation();
        var that = $(this);
        $.popupFull("<img src='" + that.attr("src") + "' />");
    });
    $(".sidebar-toggle").click(function () {
        var that = $(this), nav = that.next(".nav"), li = that.parent();
        if (nav.hasClass("hide")) {
            li.removeClass("plus").addClass("minus");
            nav.removeClass("hide");
        } else {
            li.addClass("plus").removeClass("minus");
            nav.addClass("hide");
        }
    });
    $(window).scroll(function () {
        var obj = $(".btn-group");
        var a = obj.offset().top;
        var scrolls = $(window).scrollTop();
        if (a >= scrolls && a < (scrolls + $(window).height())) {
            if (!obj.hasClass("static"))
                obj.css({
                    position: "static",
                });
        } else {
            if (!obj.hasClass("fixed"))
                obj.css({
                    position: "fixed",
                    bottom: 0,
                    "z-index": 1000
                });
        }
    });
    //页面刷新
    $(".refresh_btn").click(function () {
        window.location.reload();
    });
    //分页
    $(".input_select").click(function () {
        var ul = $("#dropdown ul");
        if (ul.css("display") == "none") {
            ul.slideDown("fast");
        } else {
            ul.slideUp("fast");
        }
    });
    $("#dropdown ul li a").click(function () {
        var txt = $(this).text();
        $(".input_select").val(txt);
        var value = $(this).attr("rel");
        $("#dropdown ul").hide();
    });
});
function merge_options(obj1, obj2) {
    var obj3 = {
    };
    for (var attrname in obj1) {
        obj3[attrname] = obj1[attrname];
    }
    for (var attrname in obj2) {
        obj3[attrname] = obj2[attrname];
    }
    return obj3;
}
function getElementsByName(tag, name) {
    var elem = document.getElementsByTagName(tag);
    var arr = new Array();
    for (i = 0, iarr = 0; i < elem.length; i++) {
        att = elem[i].getAttribute("name");

        if (att == name) {
            arr[iarr] = elem[i];
            iarr++;
        }
    }
    return arr;
}
function getCheckBoxVal(elements) {
    var value = [], j = 0;
    for (i = 0; i < elements.length; i++) {
        if (elements[i].checked) {
            value[j] = elements[i].value;
            j++;
        }
    }
    //return value.join(",");
    return value;
}
function autoResizeFrame() {
    var newheight = document.getElementById("iframe").contentWindow.document.body.scrollHeight;
    if (newheight > $(window).height() - 100) {
        newheight = $(window).height() - 100;
    }
    var width = 600;
    if ($('.table,.col-md-6', $("#iframe").contents()).length > 0) {
        $("#iframe").css({
            height: $(window).height() - 110, width: "100%"
        });
        $(".frame").css({
            "height": $(window).height() - 50, "width": "90%", "left": "5%", "top": 20
        });
    } else {
        $("#iframe").css({
            height: newheight, width: width
        });
        $(".frame").css({
            "height": newheight + 52, "width": width, "left": ($(window).width() - width) / 2, "top": ($(window).height() - newheight - 52) / 2
        });
    }

}
function inIframe() {
    try {
        return window.self !== window.top;
    } catch (e) {
        return true;
    }
}
/* gaoqh begin */
//验证是否至少选择了一条，且根据条件判断是否允许选择多条
//isCheckOne=true时，应选择且仅选择一条数据，false时应选择一条或多条数据
function isValidCheck(isCheckOne){
    if ($("input[name='check-item']:checkbox:checked").length > 0) {
        if (isCheckOne && $("input[name='check-item']:checkbox:checked").length > 1) {
            $.alert("mC003:" + jsHelper.getLanguageItemText("mC003"));
            return false;
        }
        return true;
    }
    else {
        $.alert("mC004:" + jsHelper.getLanguageItemText("mC004"));
        return false;
    }
}
/* gaoqh end */
//分页 跳转
function isPageNum(pagecount) {
    obj = $("#PageIndex").val();
    if (obj == "" || obj == "0") return false;
    if (/^[0-9]*[1-9][0-9]*$/.test(obj)) {
        if (Number(obj) > Number(pagecount)) {
            $.alert("mC007:" + jsHelper.getLanguageItemText("mC007"));
            $("#PageIndex").val("");
            return false;
        }
        return true;
    } else {
        $.alert("mC008:" + jsHelper.getLanguageItemText("mC008"));
        $("#PageIndex").val("");
        return false;
    }
}
function pagingUrl(val, url) {
    location = url + "&page=" + val;
}

function pick() {
    var date = new Date($dp.cal.newdate.y, $dp.cal.newdate.M - 1, $dp.cal.newdate.d, $dp.cal.newdate.H, $dp.cal.newdate.m, $dp.cal.newdate.s);
    $("#trxTime").val(date.Format("yyyy-MM-dd"));
    $("#formSearch").submit();
}
function clearDate() {
    $("#trxTime").val("");
    $("#formSearch").submit();
}

//检车ajax请求响应数据，如果session超时，处理跳转
function checkResDataForRedirect(r)
{
    if (typeof (res) == "string") {
        if (res.substring(0, 8) == "<script>")
            document.write(res);
    }
}
